﻿using System;
using System.Reflection;
using System.Text;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;

public static class ApplicationHelper
{
    private static TypedEventHandler<DataTransferManager, DataRequestedEventArgs> handler;
    
    static string appPackageID;
    static string friendlyName;
    static string twitterHandle;
    static string facebookProfileID;
    static string marketplaceSearchTerm;

    public static void Init(string AppPackageID,string FriendlyName = "", string TwitterHandle = "", string FacebookProfileID = "", string MarketplaceSearchTerm = "")
    {
        appPackageID = AppPackageID;
        friendlyName = FriendlyName;
        twitterHandle = TwitterHandle;
        facebookProfileID = FacebookProfileID;
        marketplaceSearchTerm = MarketplaceSearchTerm;
    }

    public static string GetApplicationVersion()
    {
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            return "version x.x.x.x";
        }

        Assembly assembly = typeof(Application).GetTypeInfo().Assembly;
        var name = new AssemblyName(assembly.FullName);
        return name.Version.ToString(4);
    }

    public static void ReviewApplication()
    {
        Windows.System.Launcher.LaunchUriAsync(
               new Uri("ms-windows-store:REVIEW?PFN=" + appPackageID));
    }

    public static void SearchMoreApps(string searchTerm)
    {
        Windows.System.Launcher.LaunchUriAsync(
       new Uri("ms-windows-store:Search?query=" + searchTerm));
    }

    public static void ViewWebsite(string websiteUrl)
    {
        Windows.System.Launcher.LaunchUriAsync(
               new Uri(websiteUrl));
    }

    public static void ConnectInTwitter(string twitterHandle)
    {
        Windows.System.Launcher.LaunchUriAsync(
               new Uri(string.Format("http://twitter.com/{0}",
                        twitterHandle)));
    }

    public static void ConnectInFacebook(string facebookProfileID)
    {
        Windows.System.Launcher.LaunchUriAsync(
               new Uri(string.Format("http://facebook.com/{0}",
                        facebookProfileID)));
    }

    public static void SupportQuestion(string toAddress, string subject, string body = null)
    {
        // Register handler for DataRequested events for sharing
        if (handler != null)
            DataTransferManager.GetForCurrentView().DataRequested -= handler;

        handler = new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>((sender,args) => 
        {
            var request = args.Request;
            request.Data.Properties.Title = subject;
            request.Data.Properties.Description = "Send feedback to " + toAddress;

            // Share recipe text
            StringBuilder builder = new StringBuilder();
            builder.Append("Please send feedback to:\r\n");
            builder.Append(toAddress);
            builder.AppendLine();

            builder.Append("--------------------------------------\r\n");
            builder.Append(body);

            request.Data.SetText(builder.ToString());

            DataTransferManager.GetForCurrentView().DataRequested -= handler;
        });
        DataTransferManager.GetForCurrentView().DataRequested += handler;

        DataTransferManager.ShowShareUI();
    }

    public static void AddCommands(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
    {
        if (!string.IsNullOrEmpty(appPackageID))
        {
            SettingsCommand rateReviewCmd = new SettingsCommand("ratereview", "rate and review", (x) =>
            {
                ReviewApplication();
            });
            args.Request.ApplicationCommands.Add(rateReviewCmd);
        }

        if (!string.IsNullOrEmpty(twitterHandle))
        {

            SettingsCommand rateReviewCmd = new SettingsCommand("twitter", string.Format("See {0} on twitter",string.IsNullOrEmpty(friendlyName) ? twitterHandle : friendlyName), (x) =>
            {
                ConnectInTwitter(twitterHandle);
            });
            args.Request.ApplicationCommands.Add(rateReviewCmd);
        }

        if (!string.IsNullOrEmpty(facebookProfileID))
        {
            SettingsCommand rateReviewCmd = new SettingsCommand("facebook", string.Format("See {0} on facebook", string.IsNullOrEmpty(friendlyName) ? facebookProfileID : friendlyName), (x) =>
            {
                ConnectInFacebook(facebookProfileID);
            });
            args.Request.ApplicationCommands.Add(rateReviewCmd);
        }

        if (!string.IsNullOrEmpty(marketplaceSearchTerm))
        {
            SettingsCommand rateReviewCmd = new SettingsCommand("market", string.Format("See other apps from {0}", string.IsNullOrEmpty(friendlyName) ? marketplaceSearchTerm : friendlyName), (x) =>
            {
                SearchMoreApps(marketplaceSearchTerm);
            });
            args.Request.ApplicationCommands.Add(rateReviewCmd);
        }
    }
}
